<?php

namespace app\admin\controller;
use app\admin\model\ArticleModel;
use app\admin\model\NewsModel;
use app\admin\model\ArticleCateModel;
use app\admin\model\MemberModel;
use app\admin\model\MemberGroupModel;
use app\admin\model\MemberStudioModel;
use app\admin\model\UserModel;
use think\Db;

class Article extends Base
{

    /**
     * [index 文章列表]
     * @author 【朱沪宁】
     */
    public function index(){
        $key = input('key');
        $map = [];
        $rolename=session('rolename');
        $this->assign('showBut',$rolename=='超级管理员'?1:0);
        if($key&&$key!==""){
            $map['title'] = ['like',"%" . $key . "%"];          
        }

        $Nowpage = input('get.page') ? input('get.page'):1;
        $limits = config('list_rows');// 获取总条数
        $count = Db::name('article')->where($map)->count();//计算总页面
        $allpage = intval(ceil($count / $limits));
        $article = new ArticleModel();
        $lists = $article->getArticleByWhere($map, $Nowpage, $limits);
        $this->assign('Nowpage', $Nowpage); //当前页
        $this->assign('allpage', $allpage); //总页数
        $this->assign('count', $count); 
        $this->assign('val', $key);

        if(input('get.page')){
            return json($lists);
        }
//        print_r($lists);die;
        return $this->fetch();
    }

    //查看
    public function find_article()
    {
        $article = new ArticleModel();   

        $id = input('param.article_id');
       
        $article = $article->getOneArticle($id);

        $this->assign('article',$article);
        return $this->fetch();
    }


    /**
     * [add_article 添加文章]
     * @return [type] [description]
     * @author 【朱沪宁】
     */
    public function add_article()
    {
        if(request()->isAjax()){

            $param = input('post.');
            $param['uid']=session('uid');
            $article = new ArticleModel();
            $flag = $article->insertArticle($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }
        return $this->fetch();
    }


    /**
     * [edit_article 编辑文章]
     * @return [type] [description]
     * @author 【朱沪宁】
     */
    public function edit_article()
    {
        $article = new ArticleModel();     
        if(request()->isAjax()){

            $param = input('post.');
           // $param["create_time"] = strtotime($param["create_time"]);  
            $flag = $article->updateArticle($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }

        $id = input('param.article_id');
        $article = $article->getOneArticle($id);

        $this->assign('article',$article);
        return $this->fetch();
    }

    /**
     * [del_article 删除文章]
     * @return [type] [description]
     * @author 【朱沪宁】
     */
    public function del_article()
    {
        $id = input('param.id');
        $cate = new ArticleModel();
        $flag = $cate->delArticle($id);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }

    /**
     * [article_state 文章状态]
     * @return [type] [description]
     * @author 【朱沪宁】
     */
    public function article_state()
    {
        $id=input('param.id');
        $status = Db::name('article')->where(array('id'=>$id))->value('status');//判断当前状态情况
        if($status==1)
        {
            $flag = Db::name('article')->where(array('id'=>$id))->setField(['status'=>0]);
            return json(['code' => 1, 'data' => $flag['data'], 'msg' => '已禁止']);
        }
        else
        {
            $flag = Db::name('article')->where(array('id'=>$id))->setField(['status'=>1]);
            return json(['code' => 0, 'data' => $flag['data'], 'msg' => '已开启']);
        }
    
    }


     

    //*********************************************分类管理*********************************************//

    public function index_add_user_cate(){

        $key = input('key');
        $id = input('id');
        $map = [];
        if($key&&$key!==""){
            $map['think_admin.username'] = ['like',"%" . $key . "%"];          
        }  
        $usermodel=new UserModel();
        $cate = new ArticleCateModel();
        $cate=$cate->getOneCate($id);
        $uid=0;
        if($cate['userid']&&$cate['userid']!==""){
            //$map['think_admin.id']=array(' notin','' .$cate['uid']. '');
            $uid=$cate['userid'];
        }
        $Nowpage = input('get.page') ? input('get.page'):1;
        $limits = config('list_rows');// 获取总条数
        $lists = $usermodel->getUsersByWhereUid($map, $Nowpage, $limits,$uid);
        $count = $usermodel->getUsersByWhereCount($map,$uid);//计算总页面
        $allpage = intval(ceil($count / $limits));
        $this->assign('Nowpage', $Nowpage); //当前页
        $this->assign('allpage', $allpage); //总页数
        $this->assign('count', $count);
        $this->assign('val', $key);
        $this->assign('cateid', $id);
        if(input('get.page')){
            return json($lists);
        }
        return $this->fetch();
    }

    public function add_userid(){
        if(request()->isAjax()){

            $param = input('post.');
            $catemodel = new ArticleCateModel();
            $cate=$catemodel->getOneCate($param['id']);
            if($cate['userid']&&$cate['userid']!==""){
                $param['userid']=$cate['userid'].",".$param['userid'];
            }
            $flag = $catemodel->editCate($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }
    }

    public function index_del_user_cate(){

        $key = input('key');
        $id = input('id');
        $map = [];
        if($key&&$key!==""){
            $map['think_admin.username'] = ['like',"%" . $key . "%"];          
        }  
        $usermodel=new UserModel();
        $cate = new ArticleCateModel();
        $cate=$cate->getOneCate($id);
        
        if($cate['userid']&&$cate['userid']!==""){
            $map['think_admin.id']=array('in','' .$cate['userid']. '');
            //$uid=$cate['userid'];
        }
        $Nowpage = input('get.page') ? input('get.page'):1;
        $limits = 500;// 获取总条数
        $lists = $usermodel->getUsersByWhereCateId($map, $Nowpage, $limits);
        
        $count = $usermodel->getUsersByWhereCateIdCount($map);//计算总页面
        $allpage = intval(ceil($count / $limits));
        $this->assign('Nowpage', $Nowpage); //当前页
        $this->assign('allpage', $allpage); //总页数
        $this->assign('count', $count);
        $this->assign('val', $key);
        $this->assign('cateid', $id);
        $this->assign('lists', $lists);
        if(input('get.page')){
            return json($lists);
        }
        return $this->fetch();
    }

    public function del_userid(){
        if(request()->isAjax()){

            $param = input('post.');
            $cate = new ArticleCateModel();
            //$cate=$cate->getOneCate($param['id']);
            $flag = $cate->editCate($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }
    }

    public function import_excel(){
        
        $zyequipment=new NewsModel();
        $list=$zyequipment->getAllNews();
        $header=array('id','create_time');
        $index=array('id','create_time');
        $filename ='自有装备';

        $this->createtable($list,$filename,$header,$index);

        
    }

    protected function createtable($list,$filename,$header=array(),$index = array()){  	
        header("Content-type:application/vnd.ms-excel");  	
        header("Content-Disposition:filename=".$filename.".xls");  	
        $teble_header = implode("\t",$header);	
        $strexport = $teble_header."\r";	
        foreach ($list as $row){  		
            foreach($index as $val){
                if($val=='assets_number'){
                    $strexport.="#".$row[$val]."\t";
                }else{
                    $strexport.=$row[$val]."\t"; 
                }			
                  		
            }		
            $strexport.="\r";  	
        }  	
        $strexport=iconv('UTF-8',"GB2312//IGNORE",$strexport);  
        exit($strexport);     
    }

}